##Replication Files for Malhotra and Tahk, "Specification Issues in Assessing the Moderating Role of Issue Importance


##Data and Variable Definitions
library(foreign)
anes <- read.spss("anes1996.POR",
                  to.data.frame=T, trim.factor.names=T)

matches <- grep("^96PR:", attr(anes, "variable.labels"))
varlab <- tolower(gsub("^96PR:", "", attr(anes, "variable.labels")[matches]))
anes <- anes[,matches]
names(anes) <- varlab

issues <- grep("impo?rta?n?t? r", names(anes), value=T)
issues <- gsub(" ?scale ?", "", gsub("impo?rta?n?t? r ", "", issues))
issues <- c("serv/spend","defense spending","help blacks",
            "abortion","envir/jobs")

r.pos.var <- 
sapply(issues,
       function(x) grep(paste("^(r )?(scale )?(protect )?", x, "( scale)?", sep=""),
                        names(anes), value=T))
r.imp.var <- 
sapply(issues,
       function(x) grep(paste("impo?rta?n?t? r", x, sep=".*"),
                        names(anes), value=T))
c.pos.var <- 
sapply(issues,
       function(x) grep(paste("^clinton ?s?c?a?l?e?", x, "s?c?a?l?e?$", sep=" ?"),
                        names(anes), value=T))
                        
r.pos <- as.matrix(as.data.frame(lapply(anes[,r.pos.var], as.numeric)))
r.pos[,4] <- ifelse(r.pos[,4] > 4, NA, r.pos[,4] + 1)
r.pos <- ifelse(matrix(r.pos-1 %in% c(1:7), ncol=5), r.pos-1, NA)

r.imp <- as.matrix(as.data.frame(lapply(anes[,r.imp.var], as.numeric)))
r.imp <- ifelse(matrix(r.imp %in% c(2:6), ncol=5), r.imp, NA)

c.pos <- as.matrix(as.data.frame(lapply(anes[,c.pos.var], as.numeric)))
c.pos[,4] <- ifelse(c.pos[,4] > 4, NA, c.pos[,4])
c.pos <- ifelse(matrix(c.pos %in% c(1:7), ncol=5), c.pos, NA)

c.feel <- anes[,"clinton thermometer"]
c.feel <- ifelse(c.feel>100, NA, c.feel)

##City Block Model

DV <- c.feel
which <- 5
RCDiff <- (r.pos-c.pos)[,which]
Pos <- ((abs(r.pos-c.pos))^1)[,which]
Imp <- (6-r.imp[,which])/4
Inter <- Pos*Imp
lm1 <- lm(DV~Pos+Imp+Inter)
lm0 <- lm(DV~Pos+Inter)
summary(lm1)
summary(lm0)
anova(lm0, lm1)                                     


## Figure 2 (Grynaviski and Corrigan Model, City Block)
pred.r <- sort(c(seq(min(RCDiff, na.rm=T), max(RCDiff, na.rm=T), length=100), 0))
pred.x <- abs(pred.r)
pred.low <- predict(lm0, data.frame(Pos=pred.x, Imp=min(Imp, na.rm=T), Inter=pred.x*min(Pos, na.rm=T)))
pred.high <- predict(lm0, data.frame(Pos=pred.x, Imp=max(Imp, na.rm=T), Inter=pred.x*max(Pos, na.rm=T)))
plot(c(pred.r, pred.r), c(pred.low, pred.high), type="n", xlab="Respondent's position minus Clinton's position", ylab="Predicted Clinton thermometer rating", main="Grynaviski and Corrigan model", ylim=c(0, 1.2*max(c(pred.low, pred.high))))
lines(pred.r, pred.low, lty=2)
lines(pred.r, pred.high, lty=1)
legend("topleft", c("Lowest importance", "Highest importance"), lty=c(2,1))
mtext("(no main effect term for importance)", 3)

## Figure 2 (Partial Utility Model, City Block)
pred.r <- sort(c(seq(min(RCDiff, na.rm=T), max(RCDiff, na.rm=T), length=100), 0))
pred.x <- abs(pred.r)
pred.low <- predict(lm1, data.frame(Pos=pred.x, Imp=min(Imp, na.rm=T), Inter=pred.x*min(Pos, na.rm=T)))
pred.high <- predict(lm1, data.frame(Pos=pred.x, Imp=max(Imp, na.rm=T), Inter=pred.x*max(Pos, na.rm=T)))
plot(c(pred.r, pred.r), c(pred.low, pred.high), type="n", xlab="Respondent's position minus Clinton's position", ylab="Predicted Clinton thermometer rating", main="Partial utility model", ylim=c(0, 1.2*max(c(pred.low, pred.high))))
lines(pred.r, pred.low, lty=2)
lines(pred.r, pred.high, lty=1)
legend("topleft", c("Lowest importance", "Highest importance"), lty=c(2,1))
mtext("(includes main effect term for importance)", 3)

##Euclidean Distance Model
DV <- c.feel
Pos <- ((abs(r.pos-c.pos))^2)[,which]
Imp <- (6-r.imp[,which])/4
Inter <- Pos*Imp
lm21 <- lm(DV~Pos+Imp+Inter)
lm20 <- lm(DV~Pos+Inter)
summary(lm21)
summary(lm20)
anova(lm21, lm20)

## Figure 3 (Grynaviski and Corrigan Model, Euclidean Distance)
pred.r <- sort(c(seq(min(RCDiff, na.rm=T), max(RCDiff, na.rm=T), length=100), 0))
pred.x <- abs(pred.r)^2
pred.low <- predict(lm0, data.frame(Pos=pred.x, Imp=min(Imp, na.rm=T), Inter=pred.x*min(Pos, na.rm=T)))
pred.high <- predict(lm0, data.frame(Pos=pred.x, Imp=max(Imp, na.rm=T), Inter=pred.x*max(Pos, na.rm=T)))
plot(c(pred.r, pred.r), c(pred.low, pred.high), type="n", xlab="Respondent's position minus Clinton's position", ylab="Predicted Clinton thermometer rating", main="Grynaviski and Corrigan model", ylim=c(0, 1.2*max(c(pred.low, pred.high))))
lines(pred.r, pred.low, lty=2)
lines(pred.r, pred.high, lty=1)
legend("topleft", c("Lowest importance", "Highest importance"), lty=c(2,1))
mtext("(no main effect term for importance)", 3)

## Figure 3 (Partial Utility Model, Euclidean Distance)
pred.r <- sort(c(seq(min(RCDiff, na.rm=T), max(RCDiff, na.rm=T), length=100), 0))
pred.x <- abs(pred.r)^2
pred.low <- predict(lm1, data.frame(Pos=pred.x, Imp=min(Imp, na.rm=T), Inter=pred.x*min(Pos, na.rm=T)))
pred.high <- predict(lm1, data.frame(Pos=pred.x, Imp=max(Imp, na.rm=T), Inter=pred.x*max(Pos, na.rm=T)))
plot(c(pred.r, pred.r), c(pred.low, pred.high), type="n", xlab="Respondent's position minus Clinton's position", ylab="Predicted Clinton thermometer rating", main="Partial utility model", ylim=c(0, 1.2*max(c(pred.low, pred.high))))
lines(pred.r, pred.low, lty=2)
lines(pred.r, pred.high, lty=1)
legend("topleft", c("Lowest importance", "Highest importance"), lty=c(2,1))
mtext("(includes main effect term for importance)", 3)
